package com.weixing.mall.provider.controller.front;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.enums.SysCfgCodeEnum;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.provider.api.model.vo.SysConfigVo;
import com.weixing.mall.provider.api.service.SysConfigServiceFeign;
import com.weixing.mall.provider.model.query.AgentBonusQuery;
import com.weixing.mall.provider.model.vo.AgentAccountVo;
import com.weixing.mall.provider.model.vo.AgentBonusVo;
import com.weixing.mall.provider.service.IAgentAccountService;
import com.weixing.mall.provider.service.IAgentBonusService;
import com.weixing.mall.util.number.NumberPatternEnum;
import com.weixing.mall.util.number.NumberToRmbUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName FrontAgentRebateController
 * @Description
 * @Author Small
 * @Date 2020/3/9 17:37
 * @Version 1.0
 **/
@RestController
@RequestMapping("front/agent/bonus")
@Api(value = "前端:代理商分红",tags = "前端:代理商分红")
public class FrontAgentBonusController extends BaseController {
    @Autowired
    private IAgentBonusService agentBonusService;

    @Autowired
    private IAgentAccountService agentAccountService;
    @Autowired
    private SysConfigServiceFeign sysConfigServiceFeign;


    @ApiOperation(value = "分红总金额")
    @GetMapping("total")
    public Result<String> rebateTotal(@MemInfo MemAuth auth) {
        long agentId = auth.getAbid();
        AgentAccountVo agentAccount=agentAccountService.selectByAgentId(agentId);
        return ResultUtil.success().setData(agentAccount.getTotalBonusRevenue());
    }

    @ApiOperation(value = "收益明细——分红")
    @GetMapping("list")
    public Result selectFrontAgentBonus(@MemInfo MemAuth auth) {
        AgentBonusQuery agentBonusQuery=new AgentBonusQuery();
        agentBonusQuery.setAgentId(auth.getAid());
        IPage<AgentBonusVo> iPage = agentBonusService.selectListOfPage(initMpPage(), agentBonusQuery);
        if (CollUtil.isNotEmpty(iPage.getRecords())) {
            Result<SysConfigVo> result = sysConfigServiceFeign.getByCode(SysCfgCodeEnum.AGENT_WITHDROW_CFG.getCode());
            if (!result.isSuccess() || WhetherEnum.NO.getValue().equals(result.getData())) {
                iPage.getRecords().forEach(t -> {
                    t.setFmtBonusAmount("*");
                    t.setFmtOrderPrice("*");
                });
            }else{
                iPage.getRecords().forEach(t -> {
                    t.setFmtBonusAmount(NumberToRmbUtil.format(t.getBonusAmount(), NumberPatternEnum.PAT_DEFAULT.getValue()));
                    t.setFmtOrderPrice(NumberToRmbUtil.format(t.getOrderPrice(),NumberPatternEnum.PAT_DEFAULT.getValue()));
                });
            }
        }
        return ResultUtil.success();
    }
}
